﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KeaoBase;

namespace KeaoMDS.Auth
{
    public partial class JueSeBianJi : BaseForm
    {
        private DataGridViewRow m_SelectRow = null;

        public JueSeBianJi()
        {
            InitializeComponent();
        }

        public JueSeBianJi(DataGridViewRow SelectRow)
        {
            InitializeComponent();
            m_SelectRow = SelectRow;
        }

        private void JueSeBianJi_Load(object sender, EventArgs e)
        {
            BindInfo();
            if (m_SelectRow != null)
            {
                try
                {
                    txt_juesemingcheng.Text = string.Format("{0}", m_SelectRow.Cells["col_juesemingcheng"].Value);
                    string[] jueSeQuanXian = string.Format("{0}", m_SelectRow.Cells["col_juesequanxian"].Value).Split(']');
                    foreach (string str in jueSeQuanXian)
                    {
                        if (string.IsNullOrEmpty(str))
                        {
                            continue;
                        }
                        lst_juesequanxian.Items.Add(str.Remove(0, 1));
                    }
                }
                catch (Exception err)
                {
                    KeaoUtils.LogTxt(string.Format("修改角色权限，绑定权限异常。{0}", err.Message));
                }

            }
        }

        private void menu_btn_baocun_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txt_juesemingcheng.Text) || lst_juesequanxian.Items.Count == 0)
                {
                    return;
                }

                if (m_SelectRow == null)
                {
                    string CreateAndCondition = KeaoUtils._DB.CreateAndCondition("juesemingcheng", DataValueCommonType.字符串, txt_juesemingcheng.Text, "", SearchConditionType.等于);
                    DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_juese", CreateAndCondition);
                    if (Dt == null)
                    {
                        return;
                    }
                    if (Dt.Rows.Count > 0)
                    {
                        KeaoUtils.ShowInfoMessage("角色名称重复，不能保存。");
                        return;
                    }

                    string jueSeQuanXian = string.Empty;
                    foreach (string str in lst_juesequanxian.Items)
                    {
                        jueSeQuanXian += string.Format("[{0}]", str);
                    }
                    TableRow newRow = new TableRow("tbl_juese", KeaoUtils.GetGUID(), DBCtl.插入);
                    newRow.DataCols.Add(new TableCol() { ColName = "juesemingcheng", ValueMember = txt_juesemingcheng.Text, ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "juesequanxian", ValueMember = jueSeQuanXian, ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "0", ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName="zhandian", ValueMember=KeaoUtils.ConfigGetNodeAttr("客户","站点"), ValueType= DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "kehubianhao", ValueMember = KeaoUtils.ConfigGetNodeAttr("客户", "客户编号"), ValueType = DataValueCommonType.字符串 });
                    
                    if (KeaoUtils._DB.SaveTableRows(newRow) > 0)
                    {
                        DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        KeaoUtils.ShowInfoMessage("保存角色信息失败。");
                    }
                }
                else
                {
                    string CreateAndCondition = KeaoUtils._DB.CreateAndCondition("juesemingcheng", DataValueCommonType.字符串, txt_juesemingcheng.Text, "", SearchConditionType.等于);
                    CreateAndCondition += KeaoUtils._DB.CreateAndCondition("id", DataValueCommonType.字符串, string.Format("{0}", m_SelectRow.Cells["col_id"].Value), "", SearchConditionType.不等于);
                    DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_juese", CreateAndCondition);
                    if (Dt == null)
                    {
                        return;
                    }
                    if (Dt.Rows.Count > 0)
                    {
                        KeaoUtils.ShowInfoMessage("角色名称重复，请检查。");
                        return;
                    }
                    string jueSeQuanXian = string.Empty;
                    foreach (string str in lst_juesequanxian.Items)
                    {
                        jueSeQuanXian += string.Format("[{0}]", str);
                    }
                    TableRow editRow = new TableRow("tbl_juese", string.Format("{0}", m_SelectRow.Cells["col_id"].Value), DBCtl.修改);
                    editRow.DataCols.Add(new TableCol() { ColName = "juesemingcheng", ValueMember = txt_juesemingcheng.Text, ValueType = DataValueCommonType.字符串 });
                    editRow.DataCols.Add(new TableCol() { ColName = "juesequanxian", ValueMember = jueSeQuanXian, ValueType = DataValueCommonType.字符串 });
                    editRow.DataCols.Add(new TableCol() { ColName="shangchuanzhuangtai", ValueMember="0", ValueType= DataValueCommonType.字符串 });
                    if (KeaoUtils._DB.SaveTableRows(editRow) > 0)
                    {
                        DialogResult = DialogResult.OK;
                    }
                }
            }
            catch (Exception err)
            {
                KeaoUtils.LogTxt(string.Format("保存角色信息异常。#{0}", err.Message));
            }
        }

        private void menu_btn_guanbi_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BindInfo()
        {
            DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_quanxian");
            if (Dt == null)
            {
                return;
            }
            foreach (DataRow row in Dt.Rows)
            {
                lst_quanxian.Items.Add(row["quanxianmingcheng"]);
            }
            lst_quanxian.SelectedIndex = 0;
        }

        private void btn_shouquan_Click(object sender, EventArgs e)
        {
            if (lst_quanxian.SelectedIndex != -1)
            {
                string str = lst_quanxian.SelectedItem.ToString();
                foreach (string quanXian in lst_juesequanxian.Items)
                {
                    if (quanXian == str)
                    {
                        return;
                    }
                }
                lst_juesequanxian.Items.Add(str);
                lst_juesequanxian.SelectedItem = str;
            }
        }

        private void btn_shanchu_Click(object sender, EventArgs e)
        {
            if (lst_juesequanxian.SelectedIndex != -1)
            {
                lst_juesequanxian.Items.RemoveAt(lst_juesequanxian.SelectedIndex);
            }
        }
    }
}
